#ifndef INMERSIONGRAFICO_H
#define INMERSIONGRAFICO_H

#include "stable.h"

inline uint qHash( const QPointF& p ) { return qHash( (ulong)p.x() + (ulong)p.y() ); }

class InmersionGrafico : public QFrame
{
    Q_OBJECT
public:
    enum Tipo {
        Profundidad,
        Temperatura,
        Presion,
        TasaCAS
    };
    InmersionGrafico( QWidget* parent, DataStore* datastore );
    Tipo showGrafico( Inmersion* inmersion );
    void setBuceador( Buceador* buceador );
public slots:
    void verMapa( bool show );
    void verProfundidad( bool checked );
    void verTemperatura( bool checked );
    void verPresion( bool checked );
    void verTasaCAS( bool checked );
protected:
    void resizeEvent( QResizeEvent* event );
    void paintEvent( QPaintEvent* event );
private:
    DataStore* _datastore;
    Buceador* _buceador;
    Inmersion* _inmersion;
    QList<Muestra*> _muestras;
    QMultiHash<QPointF,Muestra*> _marcadores;
    QPen _linepen;
    QPen _gridlinepen;
    QPen _gridspeciallinepen;
    QPen _framepen;
    QPen _marcadormasprofundopen;
    QPen _marcadormasprofundotextopen;
    QPen _marcadorpen;
    QPen _marcadortextopen;
    QPen _gridlinetextpen;
    QBrush _drawareabrush;
    QBrush _marcadorbrush;
    QBrush _marcadormasprofundobrush;
    QFont _marcadortextofont;
    QFont _gridlinetextfont;
    QRectF _drawarea;
    QRectF _grapharea;
    qreal _borderwidth;
    qreal _drawareatopmargin;
    qreal _drawareabottommargin;
    qreal _drawareasidemargin;
    qreal _graphtopmargin;
    qreal _graphbottommargin;
    qreal _topmarginpct;
    qreal _bottommarginpct;
    qreal _textmargin;
    qreal _widthfactor;
    qreal _heightfactor;
    qreal _gridheight;
    qreal _gridwidth;
    quint32 _marcadorsize;
    QLabel* _graphtitle;
    QRadioButton* _verProfundidadRadio;
    QRadioButton* _verTemperaturaRadio;
    QRadioButton* _verPresionRadio;
    QRadioButton* _verTasaCasRadio;
    Tipo _tipo;
    QGridLayout* _verlayout;
    bool _vermapa;
    QPixmap _map;
    QPixmap _scaledmap;
    QNetworkReply* _mapreply;
    QNetworkAccessManager _mapdownloader;
    void paintProfundidad();
    void paintSimpleProfundidad();
    void paintTemperatura();
    void paintSimpleTemperatura();
    void paintPresion();
    void paintTasaCAS();
    void drawGraphLine( QPainter* painter, QPainterPath* path );
    void drawGraphArea( QPainter* painter, QPainterPath* path );
    void drawGraphFrame( QPainter* painter );
    void drawProfundidadGridLines( QPainter* painter );
    void drawTemperaturaGridLines( QPainter* painter );
    void drawTiempoGridLines( QPainter* painter );
    void drawSuperficieLine( QPainter* painter, bool includetext = true );
    void drawFondoLine( QPainter* painter, bool includetext = true );
    void drawMarcadores( QPainter* painter );
    void drawPuntoMasProfundo( QPainter* painter, const QPointF& punto, Muestra* muestra );
    void drawMap();
    void scaleMap();
    void getMap();
    void addGraphHeader( bool show = true );
    void showGraphHeader( bool show, const QString& title = QString::null );
private slots:
    void mapDownloaded( QNetworkReply* reply );
signals:
    void profundidadEnabled( bool enabled );
    void temperaturaEnabled( bool enabled );
    void presionEnabled( bool enabled );
    void tasacasEnabled( bool enabled );
};

#endif // INMERSIONGRAFICO_H
